

#ifndef __MODBUS_H
#define __MODBUS_H
#include "config.h"

//#define Modbus_master
#define Modbus_slave

#define Startus_addr  				0
#define Startus_cmd					1
#define	Startus_data				2
#define	Startus_finished 			3


#define Byte_order_32_0_1_2_3		0
#define Byte_order_32_2_3_0_1		1
#define Byte_order_32_1_0_3_2		2
#define Byte_order_32_3_2_1_0		3

#define Byte_order_16_0_1			1
#define Byte_order_16_1_0			0

#define Modbus_Ready_send				0
#define Modbus_Start_send				1
#define Modbus_TX_OK_send				2
#define Modbus_RX_OK_send				3

#define Modbus_error_null			0x00
#define Modbus_error_cmd			0x01
#define Modbus_error_reg_addr		0x02
#define Modbus_error_reg_num		0x03
#define Modbus_error_crc			0x04
#define Modbus_error_addr			0x05


#define Modbus_finished_max_len		256
#define Broadcast_addr				0xaa

#define float_head_start        0
#define float_head_end          999

#define int_head_start          1000
#define int_head_end            1999

#define short_head_start        2000
#define short_head_end          2100

#define char_head_start         3000
#define char_head_end           3999

#define Coils_addr_start		4000
#define Coils_addr_end			4999


#define double_head_start       5000
#define double_head_end         5999

#define other_Head_start       6000
#define other_data_addr		   6501
#define other_Head_end         6999


struct other_struct
{
	unsigned short head;              					//6001
	unsigned short other_addr[500];          //6002    
	//unsigned int   other_data[other_reg_num];			//6502
};


struct short_struct
{
	unsigned short  head;							//2001
	unsigned short 	Addr;							//2002
	unsigned short  BaudRate;						//2003
	unsigned short	OddEven;						//2004
	unsigned short	StopBit;						//2005
	unsigned short 	Byte_order_16;					//2006
	unsigned short 	Byte_order_float;				//2007
	unsigned short  pin_reg[100];					//2008-2108

};
struct int_struct
{
    unsigned int head;              //1001-2
    
} ;
struct float_struct
{
	float head; 
	float Rpt;				// 3
	float Temperature_instant;						//5
	float Temperature;				//7
	float K_Temperature;			//9
};
struct double_struct
{
	double head;                     		//1-2-3-4
	             //13-14-15-16 ˫��������ۼ���
};


struct RealTimeData_struct
{
	float currentFlow;                 	    
	float total;                      
	float Temperature;                 	   
	float density;                   
	float zengYi;						
	unsigned short sysErr;                  
	
};

struct PaprData_struct
{
	unsigned int  currentFlow;                 	    
	unsigned int  total;                      		  
	unsigned int  Temperature;                 	   
	unsigned int  density;                                
	unsigned int  zengYi;				 
	unsigned int  sysErr;                                 
};

typedef struct 
{
	unsigned char 	*Addr;
	unsigned char 	*Byte_order_16;		   	//0:(0-1)   1:(1-0)
	unsigned char 	*Byte_order_float;		//0:(0-1-2-3) 1:(2-3-0-1) 2:(1-0-3-2) 3:(3-2-1-0)
	unsigned char 	Cmd;					        //Modbus??????  ?????????l???????
	unsigned short 	Reg_addr_start;			  //?????????l???????
	unsigned short 	Reg_num;				      //?????????l???????
	unsigned char 	Status;
	unsigned char 	Error;
	unsigned char 	Send_flag;
	unsigned char 	Status_send;			         //??????
	unsigned char 	Rx_num;					//????????
	unsigned char 	Rx_len;
	unsigned char 	Rx_len_prt;
	unsigned char	Rx_buff[Modbus_finished_max_len];
	unsigned char 	Tx_len;
	unsigned char 	Tx_buff[Modbus_finished_max_len];
	unsigned char	*Rx_data;
}Modbus_struct;

void Modbus_interrupt_char(Modbus_struct *Modbus,unsigned char input_data);
void Modbus_deal(Modbus_struct *Modbus);
void Modbus_send(Modbus_struct *Modbus);

void Modbus_Master_Read_Data(Modbus_struct *Modbus,unsigned char address,unsigned short reg,unsigned char *buf,unsigned short num);
void Modbus_Write_Data(unsigned char address,unsigned char reg,unsigned char *buf,unsigned short num);

extern struct RealTimeData_struct  RealTimeData;
extern struct short_struct  short_reg;



#endif

